<template>
    <div>
        <!-- 表格 -->
        <el-table style="width: 100%" border :data="records">
            <el-table-column type="index" width="80" label="序号" align="center"></el-table-column>
            <el-table-column prop="skuName" width="width" label="名称"></el-table-column>
            <el-table-column prop="skuDesc" width="width" label="描述"></el-table-column>
            <el-table-column prop="skuDefaultImg" width="110" label="默认图片">
                <template slot-scope="{row,$index}">
                    <img :src="row.skuDefaultImg" style="width:80px;height: 80px;">
                </template>
            </el-table-column>
            <el-table-column prop="weight" width="80" label="重量"></el-table-column>
            <el-table-column prop="price" width="80" label="价格"></el-table-column>
            <el-table-column prop="prop" width="width" label="操作">
                <template slot-scope="{row,$index}">
                    <el-button type="success" icon="el-icon-sort-down" size="mini" v-if="row.isSale == 1"
                        @click="cancel(row)"></el-button>
                    <el-button type="success" icon="el-icon-sort-up" size="mini" v-else @click="sale(row)"></el-button>
                    <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit"></el-button>
                    <el-button type="info" icon="el-icon-info" size="mini" @click="getSkuInfo(row)"></el-button>
                    <el-button type="danger" icon="el-icon-delete" size="mini"></el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页 -->
        <el-pagination style="text-align:center" :current-page="page" :page-sizes="[3, 5, 10]" :page-size="limit"
            layout="prev, pager, next, jumper,->,total,sizes" :total="total" @size-change="handleSizeChange"
            @current-change="getSkuList">
        </el-pagination>

        <!-- 抽屉 -->
        <el-drawer :visible.sync="show" size="50%" :show-close="false">
            <el-row>
                <el-col :span="5">名称</el-col>
                <el-col :span="16">{{ this.skuInfo.skuName }}</el-col>
            </el-row>
            <el-row>
                <el-col :span="5">描述</el-col>
                <el-col :span="16">{{ this.skuInfo.skuDesc }}</el-col>
            </el-row>
            <el-row>
                <el-col :span="5">价格</el-col>
                <el-col :span="16">{{ this.skuInfo.price }}元</el-col>
            </el-row>
            <el-row>
                <el-col :span="5">平台属性</el-col>
                <el-col :span="16">
                    <template>
                        <el-tag type="success" v-for="(attr, index) in skuInfo.skuAttrValueList" :key="attr.id"
                            style="margin-right: 10px;">{{ attr.attrId }}-{{ attr.valueId }}</el-tag>
                    </template>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="5">商品图片</el-col>
                <el-col :span="16">
                    <template>
                        <div class="block">
                            <el-carousel height="150px">
                                <el-carousel-item v-for="(img, index) in skuInfo.skuImageList" :key="img.id">
                                    <img :src="img.imgUrl">
                                </el-carousel-item>
                            </el-carousel>
                        </div>
                    </template>
                </el-col>
            </el-row>
        </el-drawer>
    </div>
</template>

<script>
export default {
    name: 'Sku',
    data() {
        return {
            page: 1,     //当前第几页
            limit: 10,    //当前页面有几条数据
            total: 0,     //存储分页器一共展示的数据
            records: [], //存储SKU列表的数据
            show: false,   //抽屉显示与隐藏
            skuInfo: {}
        };
    },
    mounted() {
        //获取列表数据
        this.getSkuList();
    },
    methods: {
        //获取sku列表方法
        async getSkuList(pages = 1) {
            this.page = pages;
            const { page, limit } = this;
            let result = await this.$API.sku.reqSkuList(page, limit);
            if (result.code == 200) {
                this.total = result.data.total;
                this.records = result.data.records;
            }
        },
        //改变每页展示数据数量
        handleSizeChange(limit) {
            this.limit = limit;
            this.getSkuList();
        },
        //上架
        async sale(row) {
            let result = await this.$API.sku.reqSale(row.id);
            if (result.code == 200) {
                row.isSale = 1;
                this.$message({
                    message: '上架成功',
                    type: 'success'
                })
            }
        },
        //下架
        async cancel(row) {
            let result = await this.$API.sku.reqCancel(row.id);
            if (result.code == 200) {
                row.isSale = 0;
                this.$message({
                    message: '下架成功',
                    type: 'success'
                })
            }
        },
        edit() {
            this.$message({
                message: '正在开发中',
                type: 'info'
            })
        },
        //获取Sku详情的方法
        async getSkuInfo(row) {
            this.show = true;
            let result = await this.$API.sku.reqSkuById(row.id)
            if (result.code == 200) {
                this.skuInfo = result.data;
                console.log(this.skuInfo);
            }
        }
    },
};
</script>

<style>
.el-carousel__item h3 {
    color: #475669;
    font-size: 14px;
    opacity: 0.75;
    line-height: 150px;
    margin: 0;
}

.el-carousel__item:nth-child(2n) {
    background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n+1) {
    background-color: #d3dce6;
}


</style>
<style scoped>
>>>.el-carousel__button {
    width: 10px;
    height: 10px;
    background-color: red;
    border-radius: 50%;
}

.el-row .el-col-5 {
    font-size: 18px;
    text-align: right;
}

.el-col {
    margin: 10px 10px;
}
</style>